import {DateTime} from 'luxon'
import axios from 'axios'

export declare interface FileInfo {
    fileName: string,
    filePath: string,
    size: number,
    lastModified: string | DateTime,
    isDirectory: boolean,
    isPicture: boolean
}

export declare interface Result<T> {
    code: number,
    data: T,
    msg: String
}

export async function createFolder(name: String, filePath: Array<string> | null) {
    const path: string = (filePath || []).join('/')
    await axios.post('/api/file/newFolder',null, {
        params: {
            path: `${path}/${name}`,
            userName: localStorage.getItem('userName')
        }
    })
}

function processPath(filePath: Array<string> | null): string {
    return (filePath || []).join('/')
}

export async function getFiles(filePath: Array<string> | null, userName: string): Promise<Result<Array<FileInfo>>>{
    const path = processPath(filePath)
    try {
        const resp = await axios.get('/api/file/list', {
            params: {
                userName : userName,
                path : path
            }
        })
        return resp.data
    } catch (e: any) {
        if(e.response && e.response.data) {
            return e.response.data
        }
        return {
            code: 999,
            data: [],
            msg: '未知错误'
        }
    }
}

export async function searchFiles(filePath: Array<string> | null, keyword: string):Promise<Result<Array<FileInfo>>> {
    const path = processPath(filePath)
    try {
        const resp = await axios.get('/api/file/search', {
            params: {
                userName: localStorage.getItem('userName'),
                keyword: keyword,
                path : path
            }
        })
        return resp.data
    } catch (e: any) {
        if(e.response && e.response.data) {
            return e.response.data
        }
        return {
            code: 999,
            data: [],
            msg: '未知错误'
        }
    }
}

export async function deleteFiles(files: Array<FileInfo>) {
    const paths = files.map((it) => {
        return it.filePath
    })
    await axios.post('/api/file/delete', paths, {
        params: {
            userName: localStorage.getItem('userName')
        }
    })
}

export async function copyAndParseFiles(files: Array<FileInfo>, filePath: Array<string> | null) {
    const destinationPath = processPath(filePath)
    const filePaths = files.map((it) => {
        return it.filePath
    })
    await axios.post('/api/file/copy', filePaths, {
        params: {
            destinationPath: destinationPath,
            userName: localStorage.getItem('userName')
        }
    })
}

export async function cutAndParseFiles(files: Array<FileInfo>, filePath: Array<string> | null) {
    const destinationPath = processPath(filePath)
    const filePaths = files.map((it) => {
        return it.filePath
    })
    await axios.post('/api/file/cut', filePaths, {
        params: {
            destinationPath: destinationPath,
            userName: localStorage.getItem('userName')
        }
    })
}

export async function preViewFile(filePath: String, pageNum: number = -1) {
    const resp = await axios.get('/api/file/readText', {
        params: {
            userName: localStorage.getItem('userName'),
            filePath: filePath,
            pageNum: pageNum
        },
        headers: {
            Accept: 'text/plain'
        }
    })
    return resp.data
}

export async function previewPict(filePath: String) {
    const resp = await axios.get('/api/file/readPict', {
        params: {
            userName: localStorage.getItem('userName'),
            pictPath: filePath
        },
        responseType: 'blob'
    })
    return resp.data
}